package find_code;


//插值查找
//数组的值需要比较均匀
public class InterpolationSearch {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9,10};
        int number = 3;
        System.out.println(interpolationSearch(arr, number));

    }

    public static int interpolationSearch(int[] arr,int number){
        int min = 0;
        int max = arr.length - 1;
        while(min <= max){
            int mid = min + (number - arr[min]) / (arr[max] - arr[min]) * (max - min);
            if(arr[mid] > number){
                max = mid - 1;
            } else if (arr[mid] < number) {
                min = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }
}
